package com.demo.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;

import com.demo.cms.dao.ArticleCommentDao;
import com.demo.cms.model.ArticleComment;
import com.demo.cms.service.ArticleCommentService;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.support.converter.AdjacencyListToChainedObjectConverter;

public class ArticleCommentServiceImpl implements ArticleCommentService {

	/**
	 * 评论数据访问层接口
	 */
	private ArticleCommentDao articleCommentDao;
	
	@Override
	public Long save(ArticleComment t) {
		return articleCommentDao.insert(t);
	}

	@Override
	public boolean exists(Long id) {
		return articleCommentDao.exists(id);
	}

	@Override
	public void delete(Long id) {
		articleCommentDao.delete(id);
	}

	@Override
	public void update(Long id, ArticleComment articleComment) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("id", id);
		articleCommentDao.update(articleComment, params);
	}

	@Override
	public void update(Long id, int checkStep, Boolean isChecked) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("id", id);
		ArticleComment articleComment = new ArticleComment();
		articleComment.setCheckStep(checkStep);
		articleComment.setChecked(isChecked);
		articleCommentDao.updateStep(articleComment, params);
	}
	
	@Override
	public ArticleComment find(Long id) {
		return articleCommentDao.select(id);
	}

	@Override
	public List<ArticleComment> findList(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		return articleCommentDao.select(params); 
	}

	@Override
	public ArticleComment tree(Long id, int... depths) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("id", id);
		if (depths != null && depths.length > 0) {
			List<?> depthList = CollectionUtils.arrayToList(depths);
			params.put("depthList", depthList);
		}
		List<ArticleComment> articleCommentList = articleCommentDao.tree(params);
		
		return AdjacencyListToChainedObjectConverter.conver(articleCommentList);
	}

	@Override
	public ArticleComment tree(Long id) {
		return tree(id);
	}

	@Override
	public ResultSearchResult<ArticleComment> search(Long siteId, SearchArgumentSupport args) {
		if (args.getFirstResult() < 0 || args.getMaxResults() < 1) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);

		return articleCommentDao.search(params, args);
	}

	@Override
	public ResultSearchResult<ArticleComment> search(Long siteId, String type, SearchArgumentSupport args) {
		if (args.getFirstResult() < 0 || args.getMaxResults() < 1) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("type", type);

		return articleCommentDao.search(params, args);
	}
	
	@Override
	public ResultSearchResult<ArticleComment> search(Long siteId, Long articleId, String type, SearchArgumentSupport args) {
		if (args.getFirstResult() < 0 || args.getMaxResults() < 1) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("type", type);
		params.put("articleId", articleId);
		return articleCommentDao.search(params, args);
	}


	public void setArticleCommentDao(ArticleCommentDao articleCommentDao) {
		this.articleCommentDao = articleCommentDao;
	}

}
